package Call_2;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;

public class Call {
    public static void main(String[] args) throws IOException {
        String[] aarr = null;
        ArrayList<Student> newLit = new ArrayList<>();
        ArrayList<Student> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("namesboyandgils.txt"));
        String line;
        while((line=br.readLine())!=null){
            aarr = line.split("-");
            Student s1=new Student(aarr[0], aarr[1], Integer.parseInt(aarr[2]));
            list.add(s1);

            if(Objects.equals(aarr[1], "男")){
                Student s2 = new Student(aarr[0], aarr[1], Integer.parseInt(aarr[2]) , 0.7d);
                newLit.add(s2);
            }else{
                Student s2 = new Student(aarr[0], aarr[1], Integer.parseInt(aarr[2]) , 0.3d);
                newLit.add(s2);
            }
        }
        br.close();

        BufferedWriter bw=new BufferedWriter(new FileWriter("newNames.txt"));
        for (Student s:newLit) {
            bw.write(s.toString());
            bw.newLine();
        }
        bw.close();




        double weight=0;
        for (Student student : newLit) {
            weight=weight+student.getWeight();
        }
        double[] arr=new double[newLit.size()];
        int index=0;
        for (Student student : newLit) {
            arr[index]=student.getWeight()/weight;
            index++;
        }

        for(int i=1;i<arr.length;i++){
            arr[i]=arr[i]+arr[i-1];
        }

        double number = Math.random();
        int index2 = -Arrays.binarySearch(arr, number)-1;
        Student student = newLit.get(index2);
        System.out.println(student);


    }
}
